package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class DepartmentsActivity extends Activity {

	ArrayList<NameEntity> departments;
	ArrayList<Item> departmentItems;
	MyCustomAdapter dataAdapter;
	int faculty_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_majors);
		Bundle extras = getIntent().getExtras();
		faculty_id = extras.getInt("faculty_id");
		getdepartments();
	}
	public void getdepartments() {
		String serverURL = new myURL("read_departments.php?id="+faculty_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setDeps", true);
		p.get();
	}

	public void setDeps(String s, String error) {
		Log.d("ray", "reply: " + s);
		NameEntity ne = null;
		departments = new ArrayList<NameEntity>();
		ArrayList<Department> myFs = new APIManager().getDepartments(s);
		
		for(int i =0;i<myFs.size();i++)
		{
			Log.d("ray"," Course id" +myFs.get(i).getId() + " ,name: "+myFs.get(i).getName());
			ne = new NameEntity(i, myFs.get(i).getName(), "course");
			ne.setDescription("");
			departments.add(ne);
		}
		departmentItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		if (departments.size() == 0) {
			departmentItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < departments.size(); i++) {
				Item it = new Item(departments.get(i).getId(), "",
						departments.get(i).toString());
				it.setCode("");
				departmentItems.add(it);
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.name_list,
					departmentItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						i = new Intent(getBaseContext(),CoursesActivity.class);
						Log.d("ray","sending dep: " + dataAdapter.tmpList.get(position).getId());
						i.putExtra("department_id", dataAdapter.tmpList.get(position).getId());
						
						startActivity(i);

					}
				}
			});
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.instructors , menu);
		SharedMenu.onCreateOptionsMenu(this, menu, getApplicationContext(),
				dataAdapter);
		return true;

	}
	public boolean onOptionsItemSelected(MenuItem item) {

		if (SharedMenu.onOptionsItemSelected(item, this) == false) {
			// handle local menu items here or leave blank

			switch (item.getItemId()) {
			case R.id.add:

				// get prompts.xml view
				LayoutInflater li = LayoutInflater.from(this);
				View promptsView = li
						.inflate(R.layout.prompt_addcategory, null);

				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						this);

				// set prompts.xml to alertdialog builder
				alertDialogBuilder.setView(promptsView);

				final EditText userInput = (EditText) promptsView
						.findViewById(R.id.editTextDialogUserInput);

				alertDialogBuilder
						.setCancelable(false)
						.setPositiveButton("OK",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										
										departmentItems.add(new Item(departmentItems.size()+2, "",
												userInput.getText().toString()));

										dataAdapter.currentList = departmentItems;
										dataAdapter.tmpList = departmentItems;
										dataAdapter.notifyDataSetChanged();
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
									}
								});

				final AlertDialog alertDialog = alertDialogBuilder.create();
				alertDialog
						.setOnShowListener(new DialogInterface.OnShowListener() {

							@Override
							public void onShow(DialogInterface dialog) {

								Button cancel_btn = alertDialog
										.getButton(AlertDialog.BUTTON_NEGATIVE);
								cancel_btn.setBackgroundColor(getResources()
										.getColor(R.color.turquoise));
								cancel_btn.setTextColor(getResources()
										.getColor(R.color.textview));
								Button b = alertDialog
										.getButton(AlertDialog.BUTTON_POSITIVE);
								b.setBackgroundColor(getResources().getColor(
										R.color.turquoise));
								b.setTextColor(getResources().getColor(
										R.color.textview));
							}
						});
				alertDialog.show();

			}
		}
		return super.onOptionsItemSelected(item);
	}

}
